﻿using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;
using Quantum.Core;

namespace Quantum.SignalR
{
    public abstract class SignalRConnection : IConnectionBehavior
    {
        public HubConnection Connection { get; private set; }

        public bool Connect(
            string baseUrl,
            IDictionary<string, object> clientProperties = null,
            string username = "",
            string password = "")
        {
            while (Connection == null)
            {
                var connection = new HubConnectionBuilder()
                    .WithUrl(baseUrl)
                    .WithConsoleLogger(LogLevel.Trace)
                    .Build();
                try
                {
                    connection.StartAsync().GetAwaiter().GetResult();
                    Connection = connection;
                    return true;
                }
                catch (Exception exception)
                {

                }
            }

            return false;
        }

        public void Disconnect()
        {
            Connection.DisposeAsync().GetAwaiter().GetResult();
        }
    }
}
